#!/bin/sh
################################################################################
# audio
################################################################################
. /etc/rc.d/functions

start() {

    local amixer_command
    local control
    local timeout
    local buffer=128

    /bin/touch /var/lib/init/audio.inprogress

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Starting..."

    /bin/chmod -R 777 /proc/asound
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Setting buffer to ${buffer} for /proc/asound/card${MM_AUDIO_CARD_NUMBER}/pcm${MM_AUDIO_DEVICE_NUMBER}p/sub0/prealloc"
    echo ${buffer} | tee /proc/asound/card${MM_AUDIO_CARD_NUMBER}/pcm${MM_AUDIO_DEVICE_NUMBER}p/sub0/prealloc
    current_buffer=`cat /proc/asound/card${MM_AUDIO_CARD_NUMBER}/pcm${MM_AUDIO_DEVICE_NUMBER}p/sub0/prealloc`

    if [ ! ${current_buffer} = ${buffer} ] ; then

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] ERROR: Setting buffer to ${buffer} for /proc/asound/card${MM_AUDIO_CARD_NUMBER}/pcm${MM_AUDIO_DEVICE_NUMBER}p/sub0/prealloc failed!!!"

    fi

    amixer_command="/usr/bin/amixer -c ${MM_AUDIO_CARD_NUMBER}"

    if [ ! "x${MM_AUDIO_FETCH_ASOUND_CONF}" = "xyes" ] ; then

        # Set audio card number and audio card device.
        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Adding pcm:hw${MM_AUDIO_CARD_NUMBER},${MM_AUDIO_DEVICE_NUMBER} to asound.conf..."
        /bin/sed -i "s%@MM_AUDIO_CARD_NUMBER@%${MM_AUDIO_CARD_NUMBER}%"     /etc/asound.conf
        /bin/sed -i "s%@MM_AUDIO_DEVICE_NUMBER@%${MM_AUDIO_DEVICE_NUMBER}%" /etc/asound.conf

    fi

    # Wait for audio driver to load.
    timeout=10
    while [ ! ${amixer_command} > /dev/null 2>&1 ] && [ ${timeout} -gt 0 ] ; do
        /bin/sleep 1
        timeout=$((timeout-1))
    done

    if  ! ${amixer_command} > /dev/null 2>&1 ; then

        /usr/bin/logger -s -t minimyth -p "local0.err" "[init.d/audio] error: audio driver not loaded!"
        mm_message_output err "error: MiniMyth2 can't identify/load AUDIO drivers. Please report this ..."
        sleep 20
        /bin/rm -f /var/lib/init/audio.inprogress
        return 0

    else

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] ALSA amixer successfuly operating on card ${MM_AUDIO_CARD_NUMBER}"

    fi

    if [ -e /etc/asound.state ] ; then

        /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Restoring amixer state from /etc/asound.state file..."
        /usr/sbin/alsactl --file /etc/asound.state restore

    else

        if [ ! x${MM_AUDIO_DEVICE_UCM_PROFILE} = "x" ] ; then

            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Configuring AUDIO via UCM for card \"${MM_AUDIO_DEVICE_UCM_PROFILE}\"..."
            ucm_command="/usr/bin/alsaucm -c ${MM_AUDIO_DEVICE_UCM_PROFILE}"

            if [ ! x${MM_AUDIO_DEVICE_UCM_COMMAND} = "x" ] ; then

                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] alsaucm command: \"${ucm_command} ${MM_AUDIO_DEVICE_UCM_COMMAND}\""
                alsaucm_output=`${ucm_command} ${MM_AUDIO_DEVICE_UCM_COMMAND}`

            else


                if /bin/echo "+${MM_AUDIO_TYPE}+" | /bin/grep -q '+analog+'  ; then

                    case "${MM_AUDIO_DEVICE_UCM_PROFILE}" in
                        chtrt5645)  enable_analog_audio="set _verb HiFi set _enadev Speaker" ;;
                        *)          enable_analog_audio="set _verb HiFi set _enadev Speaker" ;;
                    esac

                    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] alsaucm analog cmd.: \"${ucm_command} ${enable_analog_audio}\""
                    alsaucm_output=`${ucm_command} ${enable_analog_audio}`
                    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] alsaucm output: \"${alsaucm_output}\""

                fi

                if /bin/echo "+${MM_AUDIO_TYPE}+" | /bin/grep -q '+digital+'  ; then

                    case "${MM_AUDIO_DEVICE_UCM_PROFILE}" in
                        chtrt5645)  enable_digital_audio="set _verb HiFi set _enadev Speaker" ;;
                        *)          enable_digital_audio="set _verb HiFi set _enadev Speaker" ;;
                    esac

                    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] alsaucm digital cmd.: \"${ucm_command} ${enable_digital_audio}\""
                    alsaucm_output=`${ucm_command} ${enable_digital_audio}`
                    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] alsaucm output: \"${alsaucm_output}\""

                fi

            fi

        fi

        # Unmute audio.
        if ${amixer_command} > /dev/null 2>&1 ; then

            control=

            if /bin/echo "+${MM_AUDIO_TYPE}+" | /bin/grep -q '+analog+'  ; then

                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Unmuting all ANALOG audio outputs..."
                ${amixer_command} | /bin/grep -e '^Simple mixer control' | /bin/sed -e "s%^[^']*'%%" | /bin/sed -e "s%'[^']*$%%" |

                while read control ; do
                    case "${control}" in
                    PCM)                         ${amixer_command} set 'PCM'                       90% unmute ;;
                    Master)                      ${amixer_command} set 'Master'                    90% unmute ;;
                    Front)                       ${amixer_command} set 'Front'                     90% unmute ;;
                    Master\ Front)               ${amixer_command} set 'Master Front'              90% unmute ;;
                    Analog\ Front)               ${amixer_command} set 'Analog Front'              90% unmute ;;
                    Surround)                    ${amixer_command} set 'Surround'                  90% unmute ;;
                    Analog\ Side)                ${amixer_command} set 'Analog Side'               90% unmute ;;
                    Analog\ Rear)                ${amixer_command} set 'Analog Rear'               90% unmute ;;
                    Center)                      ${amixer_command} set 'Center'                    90% unmute ;;
                    LFE)                         ${amixer_command} set 'LFE'                       90% unmute ;;
                    Analog\ Center/LFE)          ${amixer_command} set 'Analog Center/LFE'         90% unmute ;;
                    Headphone)                   ${amixer_command} set 'Headphone'                 90% unmute ;;
                    Headphone\ Channel)          ${amixer_command} set 'Headphone Channel'         90% unmute ;;
                    esac
                done

            fi

            if /bin/echo "+${MM_AUDIO_TYPE}+" | /bin/grep -q '+digital+' ; then

                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Unmuting all DIGITAL audio outputs..."
                ${amixer_command} | /bin/grep -e '^Simple mixer control' | /bin/sed -e "s%^[^']*'%%" | /bin/sed -e "s%'[^']*$%%" |

                while read control ; do
                    case "${control}" in
                        IEC958)
                            ${amixer_command} set 'IEC958'     on
                            ${amixer_command} set 'IEC958,1'   on
                            ;;
                        IEC958\ Front)               ${amixer_command} set 'IEC958 Front'              90% unmute ;;
                        IEC958\ Side)                ${amixer_command} set 'IEC958 Side'               90% unmute ;;
                        IEC958\ Rear)                ${amixer_command} set 'IEC958 Rear'               90% unmute ;;
                        IEC958\ Center/LFE)          ${amixer_command} set 'IEC958 Center/LFE'         90% unmute ;;
                        AIU\ HDMI\ CTRL\ SRC)        ${amixer_command} set 'AIU HDMI CTRL SRC'         I2S        ;;
                    esac
                done

            fi

            if [ "x${MM_VOIP_ENABLED}" = "xyes" ] ; then

                if [ -n "${MM_AUDIO_IN_CARD_NUMBER}" ] ; then
                    amixer_command="/usr/bin/amixer -c ${MM_AUDIO_IN_CARD_NUMBER}"
                    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Unmuting Capture in card${MM_AUDIO_IN_CARD_NUMBER} and setting level to ${MM_VOIP_MIC_LEVEL}%..."
                else
                    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Unmuting Capture in card${MM_AUDIO_CARD_NUMBER} and setting level to ${MM_VOIP_MIC_LEVEL}%..."
                fi

                ${amixer_command} | /bin/grep -e '^Simple mixer control' | /bin/sed -e "s%^[^']*'%%" | /bin/sed -e "s%'[^']*$%%" |

                while read control ; do
                    case "${control}" in
                        Capture)                     ${amixer_command} set 'Capture'     ${MM_VOIP_MIC_LEVEL}% cap unmute ;;
                        Front\ Mic)                  ${amixer_command} set 'Front Mic'   ${MM_VOIP_MIC_LEVEL}% cap unmute ;;
                        Rear\ Mic)                   ${amixer_command} set 'Rear Mic'    ${MM_VOIP_MIC_LEVEL}% cap unmute ;;
                    esac
                done

                if [ -n "${MM_VOIP_MIC_BOOST_LEVEL}" ] ; then
                    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Enabling MIC Boost and setting level to ${MM_VOIP_MIC_BOOST_LEVEL}%..."
                    ${amixer_command} | /bin/grep -e '^Simple mixer control' | /bin/sed -e "s%^[^']*'%%" | /bin/sed -e "s%'[^']*$%%" |
                    while read control ; do
                        case "${control}" in
                            Mic\ Boost)              ${amixer_command} set 'Mic Boost'       ${MM_VOIP_MIC_BOOST_LEVEL}% unmute ;;
                            Front\ Mic\ Boost)       ${amixer_command} set 'Front Mic Boost' ${MM_VOIP_MIC_BOOST_LEVEL}% unmute ;;
                            Rear\ Mic\ Boost)        ${amixer_command} set 'Rear Mic Boost'  ${MM_VOIP_MIC_BOOST_LEVEL}% unmute ;;
                        esac
                    done
                fi

                # amixer -c0 set 'Input Source',0 'Rear Mic'
                # Input\ Source)           ${amixer_command} set 'Input Source',0 'Rear Mic' ;;
                if [ -n "${MM_VOIP_MIC_SELECTION}" ] ; then
                    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Selecting MIC to "${MM_VOIP_MIC_SELECTION}" ..."
                    ${amixer_command} | /bin/grep -e '^Simple mixer control' | /bin/sed -e "s%^[^']*'%%" | /bin/sed -e "s%'[^']*$%%" |
                    while read control ; do
                        case "${control}" in
                            Input\ Source)           ${amixer_command} set 'Input Source',0 "${MM_VOIP_MIC_SELECTION}" ;;
                        esac
                    done
                fi

            fi

        fi
    fi

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Started with return code=0..."

    /bin/rm -f /var/lib/init/audio.inprogress

    return 0
}

stop () {

    local amixer_command
    local control

    amixer_command="/usr/bin/amixer -c ${MM_AUDIO_CARD_NUMBER}"

    mm_message_output info "muting audio ..."

    # Mute audio.
    if ${amixer_command} > /dev/null 2>&1 ; then

        if /bin/echo "+${MM_AUDIO_TYPE}+" | /bin/grep -q '+analog+'  ; then

            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Muting all ANALOG outputs..."
            ${amixer_command} | /bin/grep -e '^Simple mixer control' | /bin/sed -e "s%^[^']*'%%" | /bin/sed -e "s%'[^']*$%%" |

            while read control ; do
                case "${control}" in
                    # General muting.
                    PCM)                ${amixer_command} set 'PCM'               0% mute ;;
                    Master)             ${amixer_command} set 'Master'            0% mute ;;
                    Front)              ${amixer_command} set 'Front'             0% mute ;;
                    Analog\ Front)      ${amixer_command} set 'Analog Front'      0% mute ;;
                    Surround)           ${amixer_command} set 'Surround'          0% mute ;;
                    Analog\ Side)       ${amixer_command} set 'Analog Sied'       0% mute ;;
                    Analog\ Rear)       ${amixer_command} set 'Analog Rear'       0% mute ;;
                    Center)             ${amixer_command} set 'Center'            0% mute ;;
                    LFE)                ${amixer_command} set 'LFE'               0% mute ;;
                    Analog\ Center/LFE) ${amixer_command} set 'Analog Center/LFE' 0% mute ;;
                    Headphone)          ${amixer_command} set 'Headphone'         0% mute ;;
                    Headphone\ Channel) ${amixer_command} set 'Headphone Channel' 0% mute ;;
                esac
            done

        fi

        if /bin/echo "+${MM_AUDIO_TYPE}+" | /bin/grep -q '+digital+' ; then

            /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Muting all Digital outputs..."
            ${amixer_command} | /bin/grep -e '^Simple mixer control' | /bin/sed -e "s%^[^']*'%%" | /bin/sed -e "s%'[^']*$%%" |

            while read control ; do
                case "${control}" in
                    # General unmuting.
                    IEC958)               ${amixer_command} set 'IEC958'            off      ;;
                    IEC958\ Front)        ${amixer_command} set 'IEC958 Front'      0% mute  ;;
                    IEC958\ Side)         ${amixer_command} set 'IEC958 Side'       0% mute  ;;
                    IEC958\ Rear)         ${amixer_command} set 'IEC958 Rear'       0% mute  ;;
                    IEC958\ Center/LFE)   ${amixer_command} set 'IEC958 Center/LFE' 0% mute  ;;
                    AIU\ HDMI\ CTRL\ SRC) ${amixer_command} set 'AIU HDMI CTRL SRC' DISABLED ;;
                esac
            done

        fi

        if [ "x${MM_VOIP_ENABLED}" = "xyes" ] ; then

            if [ -n "${MM_AUDIO_IN_CARD_NUMBER}" ] ; then

                amixer_command="/usr/bin/amixer -c ${MM_AUDIO_IN_CARD_NUMBER}"
                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Muting Capture and MIC Boost on card${MM_AUDIO_IN_CARD_NUMBER} ..."

            else

                /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/audio] Muting Capture and MIC Boost on card${MM_AUDIO_CARD_NUMBER} ..."

            fi

            ${amixer_command} | /bin/grep -e '^Simple mixer control' | /bin/sed -e "s%^[^']*'%%" | /bin/sed -e "s%'[^']*$%%" |

            while read control ; do
                case "${control}" in
                    Capture)            ${amixer_command} set 'Capture'           0% cap mute ;;
                    Front\ Mic)         ${amixer_command} set 'Front Mic'         0% cap mute ;;
                    Rear\ Mic)          ${amixer_command} set 'Rear Mic'          0% cap mute ;;
                    Mic\ Boost)         ${amixer_command} set 'Mic Boost'         0% mute ;;
                    Front\ Mic\ Boost)  ${amixer_command} set 'Front Mic Boost'   0% mute ;;
                    Rear\ Mic\ Boost)   ${amixer_command} set 'Rear Mic Boost'    0% mute ;;
                esac
            done

        fi

    fi

    return 0
}

case $1 in
    start) start ;;
    stop)  stop  ;;
esac

exit 0
